import { loadMdicMap } from '@/services/BaseLayout';
import { getBaseUrl } from '@/services/urlconst';
import {
  getCompanyID,
  getPosIDs,
  getTenantID,
  getUserDepID,
  getUserID,
  requestFunComponFiex,
} from '@/utils';
import { message } from 'antd';
import { useMemo } from 'react';
import { useRequest } from 'umi';

let baseUrl = getBaseUrl();
// let baseUrl = 'http://192.168.3.21:9001'; //小田本地

const getLoadMapUpdate = (curRes: Record<string, any>) => {
  //获取数据字典loadMdicMap
  const loadMdicMapList = useRequest(() => {
    return loadMdicMap([`${getTenantID()}1201,${getTenantID()}1202`]);
  });

  // 数据字典数据
  let mapDataList = useMemo(() => {
    if (loadMdicMapList?.data?.msgFlag) {
      return loadMdicMapList?.data?.obj;
    } else {
      return {};
    }
  }, [loadMdicMapList?.data]);

  return {
    mapDataList,
    mapLoading: loadMdicMapList?.loading,
  };
};

/**
 * 获取组织机构接口
 */
const getFlowOrgTreeByOrgIDService = () => {
  const getFlowOrgTreeByOrgID = useRequest(
    (payload: any) => {
      let reqUrl = `${getBaseUrl()}/commonController/getFlowOrgTreeByOrgID/${
        payload?.params
      }/${payload?.flowType}/${payload?.orgID}`;
      let methodType = 'GET';
      return requestFunComponFiex(methodType, reqUrl, {});
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        if (!(res && res?.msgFlag)) {
          message.error(res?.msg);
        }
      },
    },
  );

  // 处理数据
  let getFlowOrgList = useMemo(() => {
    if (getFlowOrgTreeByOrgID?.data?.msgFlag) {
      return getFlowOrgTreeByOrgID?.data?.obj;
    } else {
      return [];
    }
  }, [getFlowOrgTreeByOrgID?.data]);

  return {
    getFlowOrgTreeRun: getFlowOrgTreeByOrgID?.run,
    getFlowOrgTreeLoading: getFlowOrgTreeByOrgID?.loading,
    getFlowOrgList: getFlowOrgList,
  };
};

/**
 * 获取我的编撰
 * @param getAnnounceDataCallllback
 * @returns
 */
const getAnnounceDataSercvice = (
  getAnnounceDataCallllback: (res: any) => void,
) => {
  const getAnnounceData = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/announceController/dataGrid/${params?.params}`;
      let bodyParams = {
        // current: 1,
        // pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        ...params,
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: getAnnounceDataCallllback,
    },
  );

  return {
    getAnnounceDataRun: getAnnounceData?.run,
    getAnnounceDataLoading: getAnnounceData?.loading,
  };
};

/**
 * 新增公告
 * @param addAnnounceCallllback 新增接口调用后的回调
 * @returns
 */
const addAnnounceService = (
  addAnnounceCallllback: (res: any, callType: boolean) => void,
) => {
  const addAnnounce = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/announceController/add`;
      let methodType = 'POST';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        addAnnounceCallllback(res, true);
      },
    },
  );
  return {
    addAnnounceRun: addAnnounce?.run,
    addAnnounceLoading: addAnnounce?.loading,
  };
};

/**
 * 编辑公告
 * @param editAnnounceCallllback 新增接口调用后的回调
 * @returns
 */
const editAnnounceService = (
  editAnnounceCallllback: (res: any, callType: boolean) => void,
) => {
  const editAnnounce = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/announceController/edit`;
      let methodType = 'PUT';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        editAnnounceCallllback(res, false);
      },
    },
  );
  return {
    editAnnounceRun: editAnnounce?.run,
    editAnnounceLoading: editAnnounce?.loading,
  };
};

/**
 * 获取详情
 * @param getAnnounceByIDCall  获取详情回调
 * @returns
 */
const getAnnounceByIDService = (getAnnounceByIDCall: (res: any) => void) => {
  const getAnnounceInfo = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/announceController/getAnnounceByID`;
      let bodyParams = {
        id: params?.id,
        view: {
          viewerID: getUserID(),
          viewPositionID: getPosIDs(),
          viewDepartmentID: getUserDepID(),
          viewCompanyID: getCompanyID(),
        },
      };
      return requestFunComponFiex('POST', personUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: getAnnounceByIDCall,
    },
  );

  return {
    getAnnounceInfoRun: getAnnounceInfo?.run,
    getAnnounceLoading: getAnnounceInfo?.loading,
  };
};

/**
 * 提交审核
 * @param addCheckCallllback 接口调用后的回调
 * @returns
 */
const addCheckService = (addCheckCallllback: (res: any) => void) => {
  const addCheck = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/announceController/addCheck`;
      let methodType = 'POST';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: addCheckCallllback,
    },
  );
  return {
    addCheckRun: addCheck?.run,
    addCheckLoading: addCheck?.loading,
  };
};

/**
 * 审核
 * @param checkIssueCallllback 接口调用后的回调
 * @returns
 */
const checkIssueService = (checkIssueCallllback: (res: any) => void) => {
  const checkIssue = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        checkerID: getUserID(),
        checkPositionID: getPosIDs(),
        checkDepartmentID: getUserDepID(),
        checkCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/announceController/checkIssue`;
      let methodType = 'POST';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: checkIssueCallllback,
    },
  );
  return {
    checkIssueRun: checkIssue?.run,
    checkIssueLoading: checkIssue?.loading,
  };
};

/**
 * 审核
 * @param cancelCallllback 接口调用后的回调
 * @returns
 */
const cancelService = (cancelCallllback: (res: any) => void) => {
  const cancel = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        cancelerID: getUserID(),
        cancelPositionID: getPosIDs(),
        cancelDepartmentID: getUserDepID(),
        cancelCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/announceController/cancel`;
      let methodType = 'PUT';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: cancelCallllback,
    },
  );
  return {
    cancelRun: cancel?.run,
    cancelLoading: cancel?.loading,
  };
};

/**
 * 置顶
 * @param toTopCallllback 接口调用后的回调
 * @returns
 */
const toTopService = (toTopCallllback: (res: any) => void) => {
  const toTop = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/announceController/toTop`;
      let methodType = 'POST';
      return requestFunComponFiex(methodType, reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: toTopCallllback,
    },
  );
  return {
    toTopRun: toTop?.run,
    toTopLoading: toTop?.loading,
  };
};

export {
  getLoadMapUpdate,
  getFlowOrgTreeByOrgIDService,
  getAnnounceDataSercvice,
  addAnnounceService,
  editAnnounceService,
  getAnnounceByIDService,
  addCheckService,
  checkIssueService,
  cancelService,
  toTopService,
};
